package com.application.moneycalculator.settings;

import com.application.moneycalculator.db.MoneyCalculator;
import com.application.moneycalculator.log.Log_;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class Settings {
	
	private static final String TAG = "Settings";
	
	private static Cursor requestSkipIntroduction(Context context)
	{
		Log_.out(TAG, "requestSkipIntroduction");
		ContentResolver cr = context.getContentResolver();
		Cursor cursor = cr.query(
					MoneyCalculator.Settings.CONTENT_URI, 
					new String []{MoneyCalculator.Settings._ID, MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING_VALUE}, 
					MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING_VALUE + "=" + MoneyCalculator.Settings.SETTINGS_SKIP_INTRODUCTION, 
					null, null);
		return cursor;
	}

	public static boolean getSkipIntroduction(Context context) throws Exception
	{
		Log_.out(TAG, "getSkipIntroduction");
		
		int value = -1;
		
		Cursor cursor = requestSkipIntroduction(context);
		
		if (cursor != null && cursor.moveToFirst()) {
			
			if(cursor.getCount() > 1)
			{
				throw new Exception("Settings DB is wrong, count elements > 1");
			}

	        int valueSkipIntoruction = cursor.getColumnIndex(MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING_VALUE); 
	    
            // Get the field values
	        value = cursor.getInt(valueSkipIntoruction);
	    }
		Log_.out(TAG, "getSkipIntroduction, value [" + value + "]");
		return value == 1 ? true : false;
	}
	
	public static void setSkipIntroduction(Context context, boolean value)
	{
		Log_.out(TAG, "setSkipIntroduction, value [" + value + "]");
		
		ContentResolver cr = context.getContentResolver();
		ContentValues values = new ContentValues();
		values.clear();
		
        values.put(MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING, MoneyCalculator.Settings.SETTINGS_SKIP_INTRODUCTION);
        values.put(MoneyCalculator.Settings.SETTINGS_TABLE_FIELD_SETTING_VALUE, value ? 1 : 0);
        
        Cursor cursor = requestSkipIntroduction(context);
        
        if (cursor == null | !(cursor != null && cursor.moveToFirst()) | ( cursor != null && cursor.getCount() == 0)) {
        	Log_.out(TAG, "setSkipIntroduction, insert new row in table");
        	cr.insert(MoneyCalculator.Settings.CONTENT_URI, values);
        }
        else if(cursor!= null && cursor.getCount() == 1)
        {
        	Log_.out(TAG, "setSkipIntroduction, update existance row");
        	int valueID = cursor.getColumnIndex(MoneyCalculator.Settings._ID); 
    	    
            // Get the field values
	        int id = cursor.getInt(valueID);
        	cr.update(Uri.withAppendedPath(MoneyCalculator.Settings.CONTENT_URI, "" + id), values, null, null);
        }
        else
        {
        	Log_.out(TAG, "setSkipIntroduction, DEFECT !!!");
        }
	}
}
